package com.nf.health.app.activity.my.medicine;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.bean.DrugStoreBean;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @deprecated
 *          药房: 药房信息
 * @author yyh
 * @ TODO: 2017/5/9/009
 */

public class MyMedicineDetailActivty extends BaseActivity {
    public static  String MODEL_MYMEDICINEDETAILACTIVTY = "com.nf.health.app.activity.my.medicine.DrugStoreBean.ListBean";
    @Bind(R.id.map_view)
    MapView map_view;
    @Bind(R.id.iv_history_icon)
    CircleImageView iv_history_icon;
    @Bind(R.id.tv_medicine_store_name)
    TextView tv_medicine_store_name;
    @Bind(R.id.tv_medicine_stor_address)
    TextView tv_medicine_stor_address;
    @Bind(R.id.tv_medicine_address2)
    TextView tv_medicine_address2;
    @Bind(R.id.tv_medicine_stroe_tel)
    TextView tv_medicine_stroe_tel;
    @Bind(R.id.tv_history_detail)
    TextView tv_history_detail;
    /**
     * 百度地图对象
     */
    private BaiduMap baiduMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_mymedicinedetail);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "药店");
        DrugStoreBean.ListBean   listBean = null;
        listBean = (DrugStoreBean.ListBean) getIntent().getSerializableExtra(MODEL_MYMEDICINEDETAILACTIVTY);
        loaddata(listBean);
        init(listBean);
        map_view.getChildAt(0).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    map_view.getParent().requestDisallowInterceptTouchEvent(false);
                }else{
                    map_view.getParent().requestDisallowInterceptTouchEvent(true);
                    map_view.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
    }


    /**
     * 初始化AMap对象
     */
    private void init(DrugStoreBean.ListBean listBean) {
        //获取百度地图对象
        baiduMap = map_view.getMap();
        double lat = 0;
        double lng = 0;
        try {
            lat = Double.parseDouble(listBean.getLat());
            lng = Double.parseDouble(listBean.getLon());
            loadLatLogPosition(lat,lng);
            addMyOverLay(lat,lng);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

    }
    private void loadLatLogPosition( double lat,double lng){
        try {
//            public static final LatLng ZHENGZHOU = new LatLng(34.7466, 113.625367);// 郑州市经纬度
            LatLng cenpt = new LatLng(lat,lng);
            //定义地图状态
            MapStatus mMapStatus = new MapStatus.Builder()
                    .target(cenpt)
                    .zoom(18)
                    .build();
            //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            //改变地图状态
            baiduMap.setMapStatus(mMapStatusUpdate);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    void addMyOverLay(double lat,double lng) {
        LatLng point = new LatLng(lat, lng);
        // 构建markerOption，用于在地图上添加marker
        OverlayOptions options = new MarkerOptions()//
                .position(point)// 设置marker的位置
                .icon( BitmapDescriptorFactory.fromResource(R.drawable.iv_location_map))// 设置marker的图标
                .zIndex(9)// 設置marker的所在層級
                .draggable(true);// 设置手势拖拽
        // 在地图上添加marker，并显示
        baiduMap.addOverlay(options);
    }

    public void loaddata(DrugStoreBean.ListBean listBean){
        if(listBean == null){
            tv_medicine_store_name.setText("");

            //药店地址:
            tv_medicine_stor_address.setText("");
            tv_medicine_address2.setText("");

            //电话
            tv_medicine_stroe_tel.setText("");
            return ;
        }
        //加载药店图片
        GlideUtil.loadImgUrl(MyMedicineDetailActivty.this,iv_history_icon,listBean.getHimg(),R.drawable.default_hospital);
        String detail = "<b>简介: </b>"+ (TextUtils.isEmpty(listBean.getIntroduction()) ? "暂无" : listBean.getIntroduction());
        tv_history_detail.setText(Html.fromHtml(detail));
        //药店名字
        tv_medicine_store_name.setText(TextUtils.isEmpty(listBean.getHname()) ? "" : listBean.getHname());

        //药店地址:
        tv_medicine_stor_address.setText(TextUtils.isEmpty(listBean.getAddress()) ? "" : listBean.getAddress());
        String address = "<b>地址: </b>"+ (TextUtils.isEmpty(listBean.getAddress()) ? "暂无" : listBean.getAddress());
        tv_medicine_address2.setText(Html.fromHtml(address));

        String phone = "<b>电话: </b>"+ (TextUtils.isEmpty(listBean.getPhone()) ? "暂无" : listBean.getPhone());
        //电话
        tv_medicine_stroe_tel.setText(Html.fromHtml(phone));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mp_view.onDestroy()，销毁地图
        map_view.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mp_view.onResume ()，重新绘制加载地图
        map_view.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mp_view.onPause ()，暂停地图的绘制
        map_view.onPause();
    }

}
